﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/20 星期一 22:59:03
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;
using Sector7.Framework.EntityType;
using Sector7.Medical.Enums;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 病人数据访问对象
    /// </summary>
    public class PatientDAO : DAOBase<M7_Patient>, IPatientDAO
    {
        #region Public functions

        public override M7_Patient Read(object patientId)
        {
            string sql = @"
                SELECT
                    c.*,
                    p.*
                FROM
                    S7_Citizen c
                    LEFT JOIN M7_Patient p ON c.CitizenId = p.CitizenId
                WHERE
                    p.PatientId = @PatientId";

            M7_Patient patient = null;

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(patientId, C_ColumnName.PATIENT_ID));

                if (reader.Read())
                {
                    patient = new M7_Patient();

                    DBUtil.ReadData(reader, patient);
                }
            }

            return patient;
        }

        /// <summary>
        /// 添加病人
        /// </summary>
        /// <param name="patient">病人信息</param>
        /// <returns></returns>
        public override object Create(M7_Patient patient)
        {
            string sql = @"
                INSERT INTO M7_Patient
                        (PatientId
                        ,CitizenId
                        ,HealthCardId
                        ,IsAllergy
                        ,AnaphylactogenId)
                    VALUES
                        (@PatientId
                        ,@CitizenId
                        ,@HealthCardId
                        ,@IsAllergy
                        ,@AnaphylactogenId)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(patient.PatientId, "PatientId"),
                    DBUtil.CreateSqlParameter(patient.CitizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(patient.HealthCardId, "HealthCardId"),
                    DBUtil.CreateSqlParameter(patient.IsAllergy, "IsAllergy"),
                    DBUtil.CreateSqlParameter(patient.AnaphylactogenId, "AnaphylactogenId"));
            }
        }

        /// <summary>
        /// 根据人员编号获取病人
        /// </summary>
        /// <param name="registryNoType">登记号类型</param>
        /// <param name="registryNo">登记号</param>
        /// <returns></returns>
        public M7_Patient ReadByRegistryNo(RegistryNoType registryNoType, object registryNo)
        {
            string registryNoName = string.Empty;
            string sql = @"
                SELECT
                    *
                FROM
                    M7_Patient p
                    LEFT JOIN S7_Citizen c ON p.CitizenId = c.CitizenId
                    LEFT JOIN M7_Gender g ON c.GenderId = g.GenderId
                WHERE";

            switch (registryNoType)
            {
                case RegistryNoType.IdentityCardId:
                {
                    registryNoName = C_ColumnName.IDENTITY_ID;
                    sql += @"
                    c.{0} = @{0}";
                    sql = string.Format(sql, registryNoName);
                    break;
                }
                default:
                    break;
            }

            M7_Patient patient = null;
            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(registryNo, registryNoName));

                if (reader.Read())
                {
                    patient = new M7_Patient();
                    DBUtil.ReadData(reader, patient);

                    var gender = new M7_Gender();
                    DBUtil.ReadData(reader, gender);

                    patient.Gender = gender;
                }
            }

            return patient;
        }

        #endregion

        #region Structure
        public PatientDAO()
        {
            m_tableName = C_M7_TableName.PATIENT;
            m_primaryKeyName = C_PrimaryKeyName.PATIENT_ID;
        }
        #endregion
    }
}
